package com.newtouch.bxzs.business.statistics.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.newtouch.bxzs.business.statistics.mapper.PersonalAchievementMapper;
import com.newtouch.bxzs.business.statistics.model.bo.*;
import com.newtouch.bxzs.business.statistics.model.vo.AchievementMonthlyDetailVO;
import com.newtouch.bxzs.business.statistics.model.vo.AchievementRankingVO;
import com.newtouch.bxzs.business.statistics.model.vo.AchievementSummaryVO;
import com.newtouch.bxzs.business.statistics.service.AchievementService;
import com.newtouch.bxzs.common.base.BaseErrorEnum;
import com.newtouch.bxzs.common.base.BaseException;
import com.newtouch.bxzs.common.base.em.DistributionTypeEnum;
import com.newtouch.bxzs.common.base.em.SystemParamsEnum;
import com.newtouch.bxzs.common.base.util.RedisUtils;
import com.newtouch.bxzs.common.base.util.UserDetailUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * 个人业绩服务接口实现类
 *
 * <p>
 * <b>History:</b>
 * <table border="1">
 * <tr>
 * <th>Date</th>
 * <th>Operator</th>
 * <th>Memo</th>
 * </tr>
 * <tr>
 * <td>2021/1/7</td>
 * <td>tianshangzhu</td>
 * <td>Create</td>
 * </tr>
 * </table>
 *
 * @author tianshangzhu
 * @version 1.0.0
 * @since 1.0.0
 */
@Service
public class AchievementServiceImpl implements AchievementService {

	@Autowired
	private PersonalAchievementMapper achievementMapper;

	@Autowired
	private RedisUtils redisUtils;

	private final long YEAR_DAY = 365;

	/**
	 * 排名前十
	 */
	private final Integer RANK = 10;

	/**
	 * (non-Javadoc)
	 * <p>
	 * <p>
	 * com.newtouch.bxzs.business.statistics.service.impl.AchievementServiceImpl#
	 * getPersonalAchievementSummary(com.newtouch.bxzs.business.statistics.model.vo.
	 * AchievementSummaryVO)
	 */
	@Override
	public List<AchievementSummaryBO> getAchievementSummaryByOrg(AchievementSummaryVO achievementSummaryVO) {
		/* 判断必填内容（页数、条数、开始月份、结束月份） */
		/* 判断必填内容（页数、条数、开始月份、结束月份） */
		if (achievementSummaryVO.getStartMonth() == null
				|| achievementSummaryVO.getEndMonth() == null) {
			throw new BaseException(BaseErrorEnum.REQUEST_DATA_NULL);
		}else {
			achievementSummaryVO.setStartMonth(DateUtil.beginOfMonth(achievementSummaryVO.getStartMonth()));
			achievementSummaryVO.setEndMonth(DateUtil.endOfMonth(achievementSummaryVO.getEndMonth()));
			//校验日期
			decideDateRange(achievementSummaryVO.getStartMonth(),achievementSummaryVO.getEndMonth());
		}
		if (achievementSummaryVO.getSortType()){
			return achievementMapper.getAchievementByOrg(achievementSummaryVO,UserDetailUtil.getUserDetail().getUserJobNumber());
		}else {
			return achievementMapper.getAchievementByMonth(achievementSummaryVO,UserDetailUtil.getUserDetail().getUserJobNumber());
		}
	}


	/**
	 * (non-Javadoc)
	 * <p>
	 * <p>
	 * com.newtouch.bxzs.business.statistics.service.impl.AchievementServiceImpl#
	 * getPersonalAchievementMonthlyDetail(com.newtouch.bxzs.business.statistics.model.vo
	 * .AchievementMonthlyDetailVO)
	 */
	@Override
	public IPage<AchievementPolicyBO> getPersonalAchievementMonthlyDetail(
			AchievementMonthlyDetailVO achievementCnd) {
		if (achievementCnd.getCurrentPage() == null
				|| achievementCnd.getPageSize() == null) {
			throw new BaseException(BaseErrorEnum.REQUEST_DATA_NULL);
		}
		if (StringUtils.isEmpty(achievementCnd.getMonth()) && StringUtils.isEmpty(achievementCnd.getOrganizationCode())) {
			throw new BaseException(BaseErrorEnum.REQUEST_DATA_NULL);
		}
		//分页
		Page<AchievementPolicyBO> page = new Page<>(achievementCnd.getCurrentPage(),achievementCnd.getPageSize());
		/* 判断用户工号 */
		if (org.apache.commons.lang3.StringUtils.isEmpty(UserDetailUtil.getUserDetail().getUserJobNumber())) {
			throw new BaseException(BaseErrorEnum.USER_NOT_EXISTS);
		}
		//判断月份是否有值
		if (!StringUtils.isEmpty(achievementCnd.getMonth())){
			achievementCnd.setStartMonth(DateUtil.beginOfMonth(achievementCnd.getMonth()));
			achievementCnd.setEndMonth(DateUtil.endOfMonth(achievementCnd.getMonth()));
		}else {
			achievementCnd.setStartMonth(DateUtil.beginOfMonth(achievementCnd.getStartMonth()));
			achievementCnd.setEndMonth(DateUtil.endOfMonth(achievementCnd.getEndMonth()));
		}
		achievementCnd.setUserJobNumber(UserDetailUtil.getUserDetail().getUserJobNumber());
		if (DistributionTypeEnum.INSURANCE.getCode().equals(redisUtils.get(SystemParamsEnum.BANKING_OR_INSURANCE.getCode()))){
			return achievementMapper.getAchievementPolicyByInsurance(page,achievementCnd);
		}
		return achievementMapper.getAchievementPolicyByBank(page,achievementCnd);
	}

	/**
	 * (non-Javadoc)
	 * <p>
	 * <p>
	 * com.newtouch.bxzs.business.statistics.service.impl.AchievementServiceImpl#
	 * getPersonalAchievementPolicyDetail(com.newtouch.bxzs.business.statistics.model.vo.
	 * AchievementPolicyDetailVO)
	 */
	@Override
	public AchievementPolicyDetailBO getAchievementPolicyDetail(String policyNumber) {
		if (BeanUtil.isEmpty(policyNumber)) {
			throw new BaseException(BaseErrorEnum.REQUEST_DATA_NULL);
		}
		return achievementMapper.getAchievementPolicyDetail(policyNumber);
	}

	/**
	 * (non-Javadoc)
	 * <p>
	 * <p>
	 * com.newtouch.bxzs.business.statistics.service.impl.AchievementServiceImpl#
	 * getPersonalAchievementRankingList(com.newtouch.bxzs.business.statistics.model.vo.
	 * AchievementRankingVO)
	 */
	@Override
	public List<AchievementRankingBO> getAchievementRank(AchievementRankingVO achievementRankingVO) {
		if (achievementRankingVO.getStartMonth() == null || achievementRankingVO.getEndMonth() == null ){
			throw new BaseException(BaseErrorEnum.REQUEST_DATA_NULL);
		}
		//日期格式转换:年月转换成年月日
		achievementRankingVO.setStartMonth(DateUtil.beginOfMonth(achievementRankingVO.getStartMonth()));
		achievementRankingVO.setEndMonth(DateUtil.endOfMonth(achievementRankingVO.getEndMonth()));
		if (StringUtils.isEmpty(UserDetailUtil.getUserDetail().getUserJobNumber())){
			throw new BaseException(BaseErrorEnum.USER_NOT_EXISTS);
		}else {
			achievementRankingVO.setUserJobNumber(UserDetailUtil.getUserDetail().getUserJobNumber());
		}
		String organizationCodes = achievementMapper.getOrganizationCode(achievementRankingVO.getUserJobNumber());
		if (StringUtils.isEmpty(organizationCodes)){
			throw new BaseException(BaseErrorEnum.USER_NOT_EXISTS);
		}
		String[] organizationCode = organizationCodes.substring(1).split(",");
		return achievementMapper.getAchievementRank(achievementRankingVO,organizationCode,RANK);
	}

	/**
	 * (non-Javadoc)
	 * <p>
	 * <p>
	 * com.newtouch.bxzs.business.statistics.service.impl.AchievementServiceImpl#
	 * getProductSalesRankingList(com.newtouch.bxzs.business.statistics.model.vo.
	 * AchievementRankingVO)
	 */
	@Override
	public List<ProductSaleRankingBO> getProductSalesRankingList(AchievementRankingVO achievementRanking) {
		/* 不能为空 */
		if (StringUtils.isEmpty(achievementRanking.getStartMonth())
				|| StringUtils.isEmpty(achievementRanking.getEndMonth())
		) {
			throw new BaseException(BaseErrorEnum.REQUEST_DATA_NULL);
		}else {
			this.decideDate(achievementRanking.getStartMonth(),achievementRanking.getEndMonth());
			achievementRanking.setStartMonth(DateUtil.beginOfMonth(achievementRanking.getStartMonth()));
			achievementRanking.setEndMonth(DateUtil.endOfMonth(achievementRanking.getEndMonth()));
		}
		if (StringUtils.isEmpty(UserDetailUtil.getUserDetail().getUserJobNumber())){
			throw new BaseException(BaseErrorEnum.USER_NOT_EXISTS);
		}else {
			achievementRanking.setUserJobNumber(UserDetailUtil.getUserDetail().getUserJobNumber());
		}
		String organizationCodes = achievementMapper.getOrganizationCode(achievementRanking.getUserJobNumber());
		if (StringUtils.isEmpty(organizationCodes)){
			throw new BaseException(BaseErrorEnum.USER_NOT_EXISTS);
		}
		String[] organizationCode = organizationCodes.substring(1).split(",");
		return achievementMapper.getProductRankingAll(achievementRanking,organizationCode,RANK);
	}


	@Override
	public void decideDate(Date startTime,Date endTime){
		if (DateUtil.betweenDay(startTime,endTime,true) > YEAR_DAY){
			throw new BaseException("-1","开始时间和结束时间不能大于一年");
		}
	}


	@Override
	public void decideDateRange(Date startTime,Date endTime){
		if (DateUtil.compare(startTime,endTime) > 0){
			throw new BaseException("-1","开始日期不能大于结束日期");
		}
		String dateRange = redisUtils.get(SystemParamsEnum.QUERY_DATE_RANGE.getCode());
		if (DateUtil.betweenMonth(startTime, endTime, true) > Integer.parseInt(dateRange)){
			throw new BaseException("-1","查询日期范围不能超过"+dateRange+"月");
		}

	}

}
